#import "SlideshowManager.h"


#import "Controller.h"
#import "EditSlideshow.h"
#import "PreferencesData.h"
#import "SlideshowModel.h"


@implementation SlideshowManager

//--------------------------------------------------------------------------------------------------
- (void) awakeFromNib
{
	[mainWindow setDelegate: self];

	slideshows = [NSMutableArray arrayWithCapacity: 0];
	[slideshows retain];

	[ssTable setDataSource: self];
	[ssTable setDelegate: self];

	[self enumSlideshows];
	[ssTable reloadData];
	[mainWindow makeFirstResponder: ssTable];

	[ssTable setTarget: self];
	[ssTable setDoubleAction: @selector(editSlideshow:)];
}

//--------------------------------------------------------------------------------------------------
- (void) dealloc
{
	[slideshows release];
	[super dealloc];
}

//--------------------------------------------------------------------------------------------------
- (IBAction)addSlideshow:(id)sender
{
	EditSlideshow *ss = [[EditSlideshow alloc] initEmpty];
	[NSBundle loadNibNamed:@"EditSlideshow"  owner:ss];

	NSString *val = [[[Controller sharedController] userPreferences] stringForKey: PREF_EDIT_SLIDESHOW_FRAME_RECT];
	if (nil != val)
	{
		NSRect frame = NSRectFromString(val);
		[[ss mainWindow] setFrame: frame display:YES];
	}

	int ret = [NSApp runModalForWindow: [ss mainWindow]];
	if (1 == ret)
	{
		[self enumSlideshows];
		[ssTable reloadData];
	}
}

//--------------------------------------------------------------------------------------------------
- (IBAction)deleteSlideshow:(id)sender
{
}

//--------------------------------------------------------------------------------------------------
- (IBAction)editSlideshow:(id)sender
{
	NSIndexSet *selItems = [ssTable selectedRowIndexes];
	if ([selItems count] < 1)
	{
		return;
	}

	int index = [selItems firstIndex];
	SlideshowModel *model = [slideshows objectAtIndex: index];
	EditSlideshow *ss = [[EditSlideshow alloc] initWithModel: model];
	[NSBundle loadNibNamed:@"EditSlideshow"  owner:ss];

	NSString *val = [[[Controller sharedController] userPreferences] stringForKey: PREF_EDIT_SLIDESHOW_FRAME_RECT];
	if (nil != val)
	{
		NSRect frame = NSRectFromString(val);
		[[ss mainWindow] setFrame: frame display:YES];
	}

	int ret = [NSApp runModalForWindow: [ss mainWindow]];
	if (1 == ret)
	{
		SlideshowModel *updatedModel = [[SlideshowModel alloc] initWithFile: [model filename]];
		[slideshows replaceObjectAtIndex: index  withObject: updatedModel];
		[ssTable reloadData];
		[self updateUIFromSelection];
	}
}

//--------------------------------------------------------------------------------------------------
- (IBAction)playSlideshow:(id)sender
{
	NSIndexSet *selItems = [ssTable selectedRowIndexes];
	if ([selItems count] > 0)
	{
		SlideshowModel *model = [slideshows objectAtIndex: [selItems firstIndex]];
		[[Controller sharedController] openSlideshow: model  show:YES];
	}
}

//--------------------------------------------------------------------------------------------------
- (NSArray*) enumSlideshows
{
	if (nil == slideshows)
	{
		slideshows = [NSMutableArray arrayWithCapacity: 0];
		[slideshows retain];
	}

	// Iterate the .slideshow files in the master folder
	[slideshows removeAllObjects];

	NSString *path = [[[Controller sharedController] preferencesData] slideshowFolder];

	Controller *controller = [Controller sharedController];
	NSFileManager *fileManager = [NSFileManager defaultManager];
	NSArray *files = [fileManager directoryContentsAtPath: path];

	int index;
	int count = [files count];
	for (index = 0; index < count; ++index)
	{
		NSString *filename = [files objectAtIndex: index];
		NSString *fullPath = [path stringByAppendingPathComponent: filename];
		if (YES == [controller isSlideshowFile: fullPath])
		{
			SlideshowModel *model = [[SlideshowModel alloc] initWithFile: fullPath];
			if (nil != model)
			{
				[slideshows addObject: model];
				[model release];
			}
		}
	}

	return slideshows;
}

//--------------------------------------------------------------------------------------------------
- (void) updateUIFromSelection
{
	NSIndexSet *selItems = [ssTable selectedRowIndexes];
	int count = [selItems count];

	if (0 == count)
	{
		[labelDuration setTitleWithMnemonic: @""];
		[labelFolders setTitleWithMnemonic: @""];
		[labelIgnored setTitleWithMnemonic: @""];
		[labelName setTitleWithMnemonic: @""];
		[labelFile setTitleWithMnemonic: @""];
		[labelOrder setTitleWithMnemonic: @""];

		[buttonAdd setEnabled: NO];
		[buttonEdit setEnabled: NO];
		[buttonDelete setEnabled: NO];
		[buttonPlay setEnabled: NO];
	}
	else
	if (1 == count)
	{
		SlideshowModel *model = [slideshows objectAtIndex: [selItems firstIndex]];

		[labelName setTitleWithMnemonic: [model name]];
		[labelFile setTitleWithMnemonic: [model filename]];
		[labelOrder setTitleWithMnemonic: @""];
		[labelDuration setTitleWithMnemonic: 
			[NSString stringWithFormat: @"%d seconds", [model duration]]];

		[labelFolders setTitleWithMnemonic: [model folderListDescription]];
		[labelIgnored setTitleWithMnemonic: [model ignoreListDescription]];

		[buttonAdd setEnabled: YES];
		[buttonEdit setEnabled: YES];
		[buttonDelete setEnabled: YES];
		[buttonPlay setEnabled: YES];
	}
	else
	{
		[labelDuration setTitleWithMnemonic: @""];
		[labelFolders setTitleWithMnemonic: @""];
		[labelIgnored setTitleWithMnemonic: @""];
		[labelName setTitleWithMnemonic: @""];
		[labelFile setTitleWithMnemonic: @""];
		[labelOrder setTitleWithMnemonic: @""];

		[buttonAdd setEnabled: YES];
		[buttonEdit setEnabled: YES];
		[buttonDelete setEnabled: YES];
		[buttonPlay setEnabled: YES];
	}
}

//--------------------------------------------------------------------------------------------------
- (id)tableView
		: (NSTableView *)table
		objectValueForTableColumn: (NSTableColumn *)column
		row: (int)row;
{
	SlideshowModel *model = [slideshows objectAtIndex: row];

	if ([[column identifier] isEqual: @"Name"])
	{
		return [model name];
	}
	else
	if ([[column identifier] isEqual: @"Type"])
	{
		NSMutableString *ret = [NSMutableString stringWithCapacity: 1024];
		switch ([model sortBy])
		{
			case SS_SORT_DATE:
				[ret appendString: @"by Date"];
				break;

			case SS_SORT_NAME:
				[ret appendString: @"by Name"];
				break;

			case SS_SORT_NONE:
				[ret appendString: @"Random"];
				break;

			default:
				[ret appendFormat: @"Unknown: %d", [model sortBy]];
				break;
		}

		if (YES == [model autoShow])
		{
			[ret appendFormat: @", %d seconds", [model duration]];
		}
		else
		{
			[ret appendString: @", manual"];
		}
		return ret;
	}
	else
	if ([[column identifier] isEqual: @"Folders"])
	{
		return [model folderListDescription];
	}
	else
	{
		return [column identifier];
	}
}

//--------------------------------------------------------------------------------------------------
- (int) numberOfRowsInTableView:(NSTableView *)table
{
	return [slideshows count];
}

//--------------------------------------------------------------------------------------------------
- (void) tableViewSelectionDidChange: (NSNotification *)note
{
	[self updateUIFromSelection];
}

//--------------------------------------------------------------------------------------------------
- (NSWindow *) mainWindow
{
	return mainWindow;
}

//--------------------------------------------------------------------------------------------------
- (void) windowWillClose:(NSNotification*) notification
{
	NSUserDefaults *userPrefs = [[Controller sharedController] userPreferences];
	NSRect frame = [[self mainWindow] frame];
	[userPrefs setObject: NSStringFromRect(frame)  forKey: PREF_SLIDESHOW_MANAGER_FRAME_RECT];

	[[Controller sharedController] slideshowManagerClosing];
}

@end
